<template>
  <el-container style="height: 100vh; padding: 20px;">
    <el-header>
      <el-page-header @back="goBack" content="个人中心" />
    </el-header>
    <el-main style="display: flex; justify-content: center; align-items: center;">
      <el-card class="box-card">
        <el-form ref="adminForm" :model="adminInfo" label-width="100px">
          <el-form-item :label="store.getters.roleName === 'admin' ? '管理员账号' : '用户账号'">
            <el-tag>{{ adminInfo.username }}</el-tag>
          </el-form-item>
          <el-form-item :label="store.getters.roleName === 'admin' ? '管理员名称' : '用户名称'">
            <el-tag>{{ adminInfo.name }}</el-tag>
          </el-form-item>
          <el-form-item label="头像">
            <el-avatar :size="100" :src="adminAvatar" />
          </el-form-item>
          <el-form-item label="角色">
            <el-tag :effect="adminInfo.role === 0 ? 'dark' : 'plain'">
              {{ adminInfo.role === 0 ? (store.getters.roleName === 'admin' ? '超级管理员' : '超级用户') : (store.getters.roleName === 'admin' ? '普通管理员' : '普通用户') }}
            </el-tag>
          </el-form-item>
          <el-form-item label="联系方式">
            <el-tag>{{ adminInfo.contact ? adminInfo.contact : '无' }}</el-tag>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="editProfile">编辑资料</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-main>
    <el-drawer
        title="编辑资料"
        v-model="drawerVisible"
        direction="rtl"
        custom-class="demo-drawer"
        ref="drawer"
        size="30%"
        @close="closeDialog">
      <el-form :model="editForm" :rules="rules" ref="ruleForm" label-width="100px">
        <el-input v-model="editForm.id" autocomplete="off" type="hidden"></el-input>
        <el-form-item :label="store.getters.roleName === 'admin' ? '管理员名' : '用户名'" prop="name">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="editForm.password" autocomplete="off"></el-input>
          <el-alert
              title="不填密码则表示不修改密码"
              :closable="false"
              type="info"
              style="line-height: 12px;"
          ></el-alert>
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <Upload :pics="pics" :limit="1" @updatePics="updatePicsHandler"></Upload>
        </el-form-item>
        <el-form-item label="联系方式" prop="contact">
          <el-input v-model="editForm.contact" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveProfile">保存</el-button>
          <el-button @click="drawerVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </el-container>
</template>

<script setup>
import { ref, reactive, } from 'vue';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
import Upload from '~/Upload.vue';
import { Update as UpdateAdmin } from '@/api/admin.js'
import { Update as UpdateUser } from '@/api/user.js'

import { useStore } from 'vuex';

const store = useStore();
const router = useRouter();
const drawerVisible = ref(false);
const title = ref('编辑资料');

const adminInfo = store.getters.userInfo;

const adminAvatar = store.getters.imageUrl + adminInfo.avatar;

const editForm = reactive({
  id: null,
  name: '',
  password: '',
  avatar: '',
  contact: ''
});
// pics只是显示，此处才是写入
const updatePicsHandler = (imageUrl) => {
  editForm.avatar = imageUrl;
}
const pics = ref([]);
const rules = reactive({
  name: [
    { required: true, message: store.getters.roleName === 'admin' ? '请输入管理员名称' : '请输入用户名', trigger: 'blur' }
  ],
  contact: [
    { required: true, message: '请输入手机号/邮箱', trigger: 'blur' }
  ]
});

const editProfile = () => {
  Object.assign(editForm, { ...adminInfo});
  pics.value = [];
  pics.value.push({ url: store.getters.imageUrl + editForm.avatar })
  editForm.password = '';
  drawerVisible.value = true;
};

const saveProfile = () => {
  const updateFunction = store.getters.roleName === 'user' ? UpdateUser : UpdateAdmin;
  updateFunction(editForm).then((res) => {
    ElMessage({message: '资料更新成功，请重新登录！', type: 'success'})
    closeDialog()
    store.dispatch('logout').then(() => {
      router.replace('/login');
    });
  })
};

const closeDialog = () => {
  drawerVisible.value = false;
};

const goBack = () => {
  router.push('/home');
};
</script>

<style scoped>
.box-card{
  width: 500px;
  display: flex;
}

::v-deep(.demo-drawer .el-drawer__body) {
  padding: 20px;
}
</style>